
import { _decorator, Component, Node, Enum } from 'cc';
import { ActionManager } from '../../Action/ActionManager';
import { Game } from '../Game';
const { ccclass, property } = _decorator;

const WindowState = Enum({
    showing: -1,
    opened: -1,
    hiding: -1,
})

@ccclass('BaseView')
export class BaseView extends Component {
    private viewState = WindowState.hiding;
    onLoad() {
        ActionManager.instance.enterView(this.node.name);

        this.viewState = WindowState.hiding;
        this.load();
    }

    load() { }

    start() {
        ActionManager.instance.startView(this.viewOpen.bind(this));
        this.onStart();
    }

    onStart() { }

    viewOpen() {
        this.viewState = WindowState.opened;
        this.onViewOpen();
    }

    onViewOpen () {}

    exit() {
        if (this.viewState !== WindowState.opened) {
            return;
        }

        this.viewState = WindowState.hiding;
        ActionManager.instance.exitView(this.clear.bind(this));
    }

    clear() {
        this.beforeDestroy();
        this.node.destroy();
        Game.instance.onViewDestory(this);
    }

    beforeDestroy() { }

}